import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.prefs.BackingStoreException;
import java.util.prefs.Preferences;

import entities.User;



public class PreferencesTest {

	
	static Preferences userNode = Preferences.userNodeForPackage(PreferencesTest.class);
	
	public static void main(String[] args) throws ClassNotFoundException, IOException, BackingStoreException {
	  
//		Teste teste = load();
//		
//		System.out.println(teste.getCodigo());
		
		userNode.exportNode(new FileOutputStream("c:/temp/prefs.xml"));
		
  }
	
	
	public static User load() throws IOException, ClassNotFoundException {
	  
		byte[] byteArray = userNode.getByteArray("teste", null);
		
		ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(byteArray);
		
		ObjectInputStream objectInputStream = new ObjectInputStream(byteArrayInputStream);
		
		User teste = (User) objectInputStream.readObject();
		
		objectInputStream.close();
		
		
		return teste;
		
  }
	
	public static void save() throws IOException, BackingStoreException {
		
		ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); 
		
		ObjectOutputStream objectOutputStream = new ObjectOutputStream(byteArrayOutputStream);
		
		User teste = new User();
		teste.setCodigo(10L);
		
		objectOutputStream.writeObject(teste);
		
		objectOutputStream.close();
		
		byte[] byteArray = byteArrayOutputStream.toByteArray();
		
		userNode.putByteArray("teste", byteArray);
		
		userNode.flush();
		
		
  }
	
}
